package com.tianxinweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianxinweb.entity.Admin;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.mapper.AdminMapper;
import com.tianxinweb.service.AdminService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tianxinweb.utils.IdGen;
import com.tianxinweb.utils.Md5Util;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cboy
 * @since 2018-07-19
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin getAdminByName(String adminName) {
        return selectOne(new EntityWrapper<Admin>().eq("admin_name",adminName));
    }

    @Override
    public Page<Admin> getAdminList(Integer pn, Integer size, String key) {
        if("".equals(key.trim())){
            return selectPage(new Page<>(pn,size,"admin_id",false),new EntityWrapper<Admin>()
                            .ne("admin_name","admin")
                            .setSqlSelect("admin_id","admin_name","last_login_ip","last_login_time","login_count","create_time","admin_status"));
        }
        return selectPage(new Page<>(pn,size,"admin_id",false),
                new EntityWrapper<Admin>()
                        .ne("admin_name","admin")
                        .setSqlSelect("admin_id","admin_name","last_login_ip","last_login_time","login_count","create_time","admin_status")
                        .like("admin_name",key)
        );

    }

    @Override
    public void addAdmin(Admin admin) {
        Set<Integer> set = new HashSet<>();
        if(admin.getRoleIds() != null && !"".equals(admin.getRoleIds())){
            try{
                String[] ids = admin.getRoleIds().split(",");
                for(String id : ids){
                    set.add(Integer.parseInt(id));
                }
            }catch (Exception e){
                //不用处理
            }
        }
        Admin saveAdmin = new Admin();
        if(!set.isEmpty()){
            saveAdmin.setRoleIds(JSON.toJSONString(set));
        }
        String loginSalt = IdGen.getRandomString(16);
        String md5LoginPwd = Md5Util.generate(admin.getAdminPwd(), loginSalt);
        saveAdmin.setAdminPwd(md5LoginPwd);
        saveAdmin.setPwdSalt(loginSalt);
        saveAdmin.setLastLoginTime(new Date());
        saveAdmin.setCreateTime(new Date());
        saveAdmin.setAdminName(admin.getAdminName());

        insert(saveAdmin);
    }

    @Override
    public void updateAdminPriv(Admin admin, String pids) {
        admin.setPrivIds("");
        try{
            Set<Integer> idlist = new HashSet<>();
            String[] ids = pids.split(",");
            for(String id : ids){
                idlist.add(Integer.parseInt(id));
            }
            if(!idlist.isEmpty()) admin.setPrivIds(JSON.toJSONString(idlist));
        }catch (Exception e){
            //noting to do
        }

        updateById(admin);
    }

    @Override
    public void updateAdminRole(Admin admin, String ids) {
        admin.setPrivIds("");
        try{
            Set<Integer> idlist = new HashSet<>();
            String[] idArr = ids.split(",");
            for(String id : idArr){
                idlist.add(Integer.parseInt(id));
            }
            if(!idlist.isEmpty()) admin.setRoleIds(JSON.toJSONString(idlist));
        }catch (Exception e){
            //noting to do
        }

        updateById(admin);

    }

}
